<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/console/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/console/lib/css/public.css" media="all">
    <link rel="stylesheet" href="/console/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="/editor/css/style.css" />
    <link rel="stylesheet" href="/editor/css/editormd.css" />
    <style>
        body {
            background-color: #ffffff;
        }
        #treeView {
            width: 600px;
            margin: 20px auto 0;
            border: 1px solid #f2f2f2;
        }
        .treeNode {
            height: 32px;
            border: 1px solid #fff;
            border-width: 1px 0;
            cursor: pointer;
            font-size: 0;
        }
        .treeNode:hover {
            background: #f4f9fd;
            border-color: #e5f0fb;
        }
        .treeNode-cur,
        .treeNode-cur:hover {
            background: #e5f0fb;
            border-color: #BBD4EF #fff;
        }
        .icon {
            display: inline-block;
            background: url(icon.png) no-repeat;
            vertical-align: middle;
        }
        .icon-minus {
            width: 15px;
            height: 15px;
            margin: 0 4px;
            background-position: -30px -22px;
        }
        .icon-add {
            width: 15px;
            height: 15px;
            margin: 0 4px;
            background-position: -30px 0;
        }
        .icon-file {
            width: 22px;
            height: 18px;
            margin-right: 5px;
            background-position: 0 -22px;
        }
        .title {
            position: relative;
            top: 6px;
            font-size: 14px;
        }

        .treeNode-empty .icon-minus {
            background-position: -999px -999px;
        }
        .treeNode-empty .icon-add {
            background-position: -999px -999px;
        }

        #treeView ul.none {
            display: none;
        }
    </style>
</head>
<body>
<div>

    <div style="padding-left: 100px;padding-right: 100px">

        <div style="padding-left: 30px;padding-right: 30px">

            <form class="layui-form layui-form-pane"  lay-filter="example" onsubmit="return false;" action="">
                <div style="padding-top: 10px;padding-bottom: 10px ">
                    <button class="layui-btn"  lay-submit lay-filter="formDemo">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>

                <hr>

                <input type="hidden" name="pid"    placeholder="请输入pid" autocomplete="off" class="layui-input" value="0">
                <input type="hidden" name="ismarker"     autocomplete="off" class="layui-input" value="true">

                <div class="layui-form-item">
                    <label class="layui-form-label">标题名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" required  lay-verify="required" placeholder="请输入栏目名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">标签名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="tag" required  lay-verify="required" placeholder="请输入标签名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <input type="hidden" name="channelId" required  lay-verify="required" placeholder="请输入标签名称" autocomplete="off" class="layui-input">
                <div class="layui-form-item">
                    <label class="layui-form-label">栏目名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="channelIdName" required  lay-verify="required" placeholder="请输入栏目名称" autocomplete="off" class="layui-input">
                    </div>
                </div>

            </form>
        </div>
        <div style="margin: 50px 0 50px 0;padding-left: 30px;padding-right: 30px">
            <div id="test-editormd">
                <textarea id="content-main" style="display:none;"></textarea>
            </div>
        </div>




    </div>

</div>
<div style="display: none" class="layuitree">
    <div id="treeView"></div>
</div>
<script src="/console/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/console/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/js/jquery-1.12.1.min.js"></script>
<script src="/js/common.js" charset="utf-8"></script>
<script src='/js/tools.js'></script>
<script src='/js/tree.js'></script>
<script src="/editor/editormd.min.js"></script>
<script>

    layui.use(['layer','form'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer;


        var testEditor;

        $(function() {
            testEditor = editormd("test-editormd", {
                width   : "100%",
                height  : 640,
                syncScrolling : "single",
                path    : "/editor/lib/"
            });
        });

        var url = decodeURI(window.location.href);
        var argsIndex = url.split("?obj=");
        var arg = argsIndex[1];

        let loginValited=api.valitLogin();
        if(loginValited.code===0){
            let info=loginValited.data;
            if(info.code!==200 && !info.isLogin){
                location.href="/console/login.html";
            }else {
                let username=loginValited.data.username;
                $("#username").html(username);
            }
        }




        if(arg!==undefined && arg!==''){
            let obj=api.getchannelone(arg);
            if(obj.code===0){
                //表单赋值
                form.val('example',{
                    channelId:obj.data.id,
                    channelIdName:obj.data.channelName
                });
            }
        }


        $('input[name="channelIdName"]').click(function() {
            var index = layer.open({
                title: '选择栏目',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                btn:['确定','取消'],
                area: ['32%', '70%'],
                content: $(".layuitree"),
                end:function(){

                }
            });
        });



        //监听提交
        form.on('submit(formDemo)', function (data) {
            let obj=data.field;
            obj.content=$("#content-main").val();
            let res=api.saveContent(obj)
            layer.msg(res.msg)
            if(res.code===0){
                location.href="../../page/title/updatemarkerdowncontent.html?obj="+res.data.id;
            }
        });


        let res={};
        let obj=api.getchanneltreelist(res);

        var treeView = tools.$('#treeView');
        var fileData = obj.data;


        treeView.innerHTML = treeHtml(fileData, -1);

        // 浜嬩欢
        var fileItem = tools.$('.treeNode');
        var root_icon = tools.$('.icon-control', fileItem[0])[0];

        root_icon.className = 'icon icon-control icon-minus';

        tools.each(fileItem, function (item) {
            filesHandle(item,fileData);
        });



        $(".treeNode").click(function() {
            let id=$(this).attr('data-file-id');
            let value=$(this).attr('data-file-value');
            console.log(id+" "+value)
            //表单赋值
            form.val('example',{
                channelId:id,
                channelIdName:value,
            });
        })


    });


</script>
</body>
</html>